<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<style>
  .container1 {
    width: 100%;
    height: 100px;
    align-items: center;

    /*元素水平居中*/
    margin-top: 30px;
    margin-bottom: 30px;

  }

  .first {
    display: flex;
    flex-direction: row;
    text-align: left;
  }

  .name1 {
    font-size: 16px;
  }

  .name2 {
    font-size: 16px;
  }

  .name3 {
    font-size: 16px;
  }



  .button1 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .button2 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .button3 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .button4 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .container2 {
    width: 100%;
    height: 600px;
    align-items: center;

    /*边框*/
    /*元素水平居中*/
  }

  .demo-ruleForm {
    width: 30%;
  }

  .block {
    text-align: center;
  }
</style>

<body>

  <div id="app">

    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="饲料idsn">
        <el-input v-model="formInline.idsn" placeholder="饲料idsn"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search2">查询</el-button>
      </el-form-item>
      <el-form-item label="饲料购买日期大于：">
        <el-date-picker v-model="formInline.date" type="date" value-format="yyyy-MM-dd" placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search">查询</el-button>
        <el-button type="primary" @click="getall">全部</el-button>

      </el-form-item>
    </el-form>
    <div class="container2">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="饲料列表" name="first">
          <el-table ref="multipleTable" border :data="filtedFeedlist" tooltip-effect="dark" style="width: 100%"
            @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column prop="createdate" label="购买日期" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="idsn" label="饲料IDSN" width="120">
            </el-table-column>
            <el-table-column prop="info" label="饲料描述" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="cost" label="饲料成本" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="total" label="饲料总量" show-overflow-tooltip>
            </el-table-column>

            <el-table-column fixed="right" label="饲料剩余量" width="220">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.remain" size="small" :step="50" :min="50" label="饲料减少量">
                </el-input-number>
                <el-button size="small" @click.native.prevent="updatefeed(scope.$index, filtedFeedlist)">
                  提交
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="添加饲料" name="second">
          <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="饲料IDSN" prop="name">
              <el-input-number v-model="ruleForm.idsn" label="IDSN"></el-input-number>
            </el-form-item>

            <el-form-item label="饲料总量">
              <el-input v-model="ruleForm.total"></el-input>
            </el-form-item>
            <el-form-item label="饲料剩余">
              <el-input v-model="ruleForm.remain"></el-input>
            </el-form-item>

            <el-form-item label="饲料花费">
              <el-input v-model="ruleForm.cost"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="createfeed" type="success" size="small">创建饲料 </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>

</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.bootcss.com/axios/0.19.2/axios.js"></script>

<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
  new Vue({
    el: '#app',
    data: {
      eated: 0,
      alertUser: false,
      alertForm: {
        id: 2,
        name: '',
        password: '',
        phone: '',
        address: '',
        info: '',
      },
      ruleForm: {
        idsn: '',
        cost: '',
        total: '',
        remain: ''
      }, options: [{
        value: '4',
        label: '销售员'
      },],

      formInline: {
        idsn: '',
        date: ''
      },

      activeName: 'first',
      feedList: [],
      filtedFeedlist: [],
      multipleSelection: [],
      currentPage3: 5,
    },
    mounted() {
      this.getFeed();
    },
    methods: {
      updatefeed(index, feedlist) {
        var parm = { id: feedlist[index].id, remain: feedlist[index].remain - this.eated }
        console.log(parm)
        axios.post("/feed/eatFeed", parm).then(res => {
          console.log(res.data)
          if (res.data.result == "1001") {
            this.$notify.success({
              title: '成功',
              message: '修改信息成功'
            })
            console.log(res.data)
            return;
          } else {
            this.$notify.error({
              title: '失败',
              message: '饲料修改失败'
            })
          }
        })
      },
      createfeed() {
        axios.post("/feed/addFeed", this.ruleForm).then(res => {
          console.log(res.data)
          if (res.data.result == "1001") {
            this.$notify.success({
              title: '成功',
              message: '添加饲料成功'
            })
            this.getFeed();
            console.log(res.data)
            return;
          } else {
            this.$notify.error({
              title: '失败',
              message: '添加饲料失败'
            })
          }
        })
      },
      getall() {
        this.filtedFeedlist = this.feedList;
      },
      search() {
        var newlist = []
        var searchdate = new Date(this.formInline.date)
        this.feedList.forEach(ele => {
          var eledate = new Date(ele.createdate.substr(0, 10));
          if (eledate > searchdate) {
            newlist.push(ele)
          }
        })
        this.filtedFeedlist = newlist;
      },
      search2() {
        var newlist = []
        this.feedList.forEach(ele => {
          if (ele.idsn == this.formInline.idsn) {
            newlist.push(ele)
          }
        })
        this.filtedFeedlist = newlist;
      },
      getFeed() {
        this.feedList = []
        axios.post("/feed/getAllFeed", {}).then(res => {
          console.log(res);
          if (res.data.result == "1001") {
            res.data.data.forEach(ele => {
              this.feedList.push(ele)
            })
            this.filtedFeedlist = this.feedList;
          }

          console.log(this.userList)
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      handleClick(tab, event) {

      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      deleteRow(index, rows) {
        axios.post("/deluser", { name: rows[index].name }).then(res => {
          console.log(res);
          if (res.data.result == "ok")
            rows.splice(index, 1)
          this.$notify.success({
            title: '删除成功',
            message: '删除成功！'
          })
        })
      },
      alertRow(index, row) {
        this.alertUser = true;
        this.alertForm.id = row[index].id;
        this.alertForm.name = row[index].name;
        this.alertForm.phone = row[index].phone;
        this.alertForm.address = row[index].address;
        this.alertForm.info = row[index].info;
        this.alertForm.password = row[index].password;
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
      }
    }
  })
</script>

</html>